import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/personal_center/my_collect_data.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class CollectionItem extends StatefulWidget {
  @override
  _CollectionItemState createState() => _CollectionItemState();
  final MyCollectData item;
  final Function getData;
  CollectionItem({this.item, this.getData});
}

class _CollectionItemState extends State<CollectionItem> {
  @override
  Widget build(BuildContext context) {
    return Slidable(
      actionPane: SlidableDrawerActionPane(),
      actionExtentRatio: 0.55,
      child: Container(
          color: Colors.white,
          padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
            decoration: BoxDecoration(
              border: Border(
                  bottom:
                      BorderSide(color: rgba(235, 235, 235, 0.5), width: 1)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                widget.item.fileUrls.length > 0
                    ? Container(
                        padding: EdgeInsets.only(right: 10),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(6)),
                        child: Image.network(
                          '${widget.item.fileUrls[0]}',
                          width: 112,
                          height: 80,
                        ),
                      )
                    : G.emptyBox(),
                Expanded(
                  child: Container(
                    height: 80,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '${widget.item.title}',
                          style: TextStyle(
                              fontSize: 15, color: rgba(68, 68, 68, 1)),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                        Padding(
                            padding: EdgeInsets.only(bottom: 4),
                            child: LabelCollect(name: '新闻'))
                      ],
                    ),
                  ),
                )
              ],
            ),
          )),
      secondaryActions: <Widget>[
        IconSlideAction(
          caption: '取消',
          color: rgba(248, 82, 81, 1),
          icon: Icons.delete,
          onTap: () => cancelCollect(widget.item.id),
        ),
      ],
    );
  }

  //取消收藏
  cancelCollect(String id) async {
    print('$id');
    var result = await G.req.news.cancelNewsCollect(objId: id, objType: 0);
    print('收藏结果$result');
    if (result.data['status'] == 1) {
      G.toast('取消成功');
      widget.getData();
    }
  }
}
